package com.liveneo.adapter.mobile.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liveneo.adapter.base.entity.PrpmItemImgRela;
import com.liveneo.adapter.base.entity.PrpmItemVideoRela;
import com.liveneo.adapter.base.entity.PrpmUploadImage;
import com.liveneo.adapter.base.entity.PrpmUploadLossItemImg;
import com.liveneo.adapter.base.entity.PrpmUploadOcrImg;
import com.liveneo.adapter.base.entity.PrpmUploadVideo;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.base.support.OssManagerTool;
import com.liveneo.adapter.mobile.model.BaseResponse;
import com.liveneo.adapter.mobile.model.dto.LossItemDto;
import com.liveneo.adapter.mobile.model.dto.MobileImageRemarkDto;
import com.liveneo.adapter.mobile.model.request.LossConfirmRequest;
import com.liveneo.adapter.mobile.model.request.UploadImageRemarkRequest;
import com.liveneo.adapter.mobile.model.request.UploadImageSurveyRequest;
import com.liveneo.adapter.mobile.model.request.UploadImageSurveyServlet78Request;
import com.liveneo.adapter.mobile.model.request.UploadOcrImageServlet51Request;
import com.liveneo.adapter.mobile.model.request.UploadVideoRequest;
import com.liveneo.adapter.mobile.model.response.CheckImageRemarkResponse;
import com.liveneo.adapter.mobile.model.response.UploadImageSurveyServlet78Response;
import com.liveneo.adapter.mobile.model.response.UploadOcrImageServlet51Response;
import com.liveneo.adapter.mobile.model.response.UploadVideoResponse;
import com.liveneo.adapter.mobile.service.MobileCaseService;
import com.liveneo.adapter.mobile.service.MobileImageService;
import com.liveneo.adapter.mobile.service.PrpmItemImgRelaService;
import com.liveneo.adapter.mobile.service.PrpmItemVideoRelaService;
import com.liveneo.adapter.mobile.service.PrpmUploadLossItemImgService;
import com.liveneo.adapter.mobile.service.PrpmUploadService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.DateUtil;
import com.liveneo.adapter.mobile.util.Str;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.common.utils.UUIDUtil;
import com.liveneo.system.base.utils.DateUtils;
import com.liveneo.system.base.utils.EncryptUtil;
import com.liveneo.system.base.utils.JsonUtils;
import com.liveneo.system.base.utils.StringUtils;

/**
 * @Description: 移动端拍照查勘相关接口
 * @return TODO
 * @author liveneo-shuqw
 * @date 2016年6月13日 下午3:50:28
 * Copyright: Copyright (c) 2015
 * Company:北京朗泰
 * @version 3.0
 */
@Controller
@RequestMapping("/mobile")
public class MobileImageController extends BaseController {
    @Autowired
    private MobileImageService           mobileImageService;
    @Autowired
    private MobileCaseService            mobileCaseService;
    @Resource
    private OssManagerTool               ossManagerTool;
    @Autowired
    private PrpmUploadService            prpmUploadService;
    @Autowired
    private PrpmUploadLossItemImgService prpmUploadLossItemimgService;
    @Autowired
    private PrpmItemImgRelaService       itemImgRelaService;
    @Autowired
    private PrpmItemVideoRelaService     itemVideoRelaService;

    /**
     * MP31
     * 1.上传手机进行的拍照查勘案件的备注信息
     * 
     * @Title：拍照查勘-上传备注信息
     * @author zhangyw 2016年10月12日 下午1:44:34
     * @param data
     * @return String
     */
    @RequestMapping("/uploadCheckImageRemark.action")
    @ResponseBody
    public String uploadCheckImageRemark(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP31入参=" + loguuid;
        String result = null; // 结果
        BaseResponse response = null;
        try {
            // 获取参数
            UploadImageRemarkRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, UploadImageRemarkRequest.class);
            // 保存图片查勘信息
            int i = mobileImageService.saveUploadImageRemark(request);
            response = MobileReturnCodeSupport.buildBaseReponse(i == 1 ? MobileReturnCodeSupport.CODE_SUCCESS : MobileReturnCodeSupport.KEY_MOBILE_00035,
                    MobileReturnCodeSupport.SYS_CODE, null);
        } catch (Exception e) {
            LogUtils.error("MP31--拍照查勘上传备注--失败，", e);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            // 返回结果封装
            result = JsonUtils.toJson(response);
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) LogUtils.info("MP31--拍照查勘上传备注信息,响应结果：" + result);
            else LogUtils.error("MP31--拍照查勘上传备注信息,响应结果：" + result);
            // 加密返回
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * 
     * MP32
     * 1.通过平台获取拍照查勘案件的备注信息
     * 
     * @Title：拍照查勘-获取备注信息
     * @author zhangyw 2016年10月14日 上午9:59:55
     * @param data
     * @return String
     */
    @RequestMapping("/getCheckImageRemark.action")
    @ResponseBody
    public String getCheckImageRemark(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP32入参=" + loguuid;
        String result = null; // 结果
        CheckImageRemarkResponse response = null;
        try {
            LossConfirmRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LossConfirmRequest.class);
            // 获取列表
            // List<MobileImageRemarkDto> remarkList = mobileImageService.getRemarkByLossUuid(request.getLossUuid());
            List<MobileImageRemarkDto> remarkList = mobileImageService.getRemarkByRequest(request);
            List<MobileImageRemarkDto> remarkListNew = new ArrayList<MobileImageRemarkDto>();
            for (MobileImageRemarkDto imageRemarkDto : remarkList) {
                MobileImageRemarkDto imageRemarkDtoNew = new MobileImageRemarkDto();
                BeanUtils.copyProperties(imageRemarkDto, imageRemarkDtoNew);
                if (imageRemarkDto.getUserCode().equals(request.getUserCode())) {
                    imageRemarkDtoNew.setIsSelfMsg("1");
                } else {
                    imageRemarkDtoNew.setIsSelfMsg("2");
                }
                remarkListNew.add(imageRemarkDtoNew);
            }
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    CheckImageRemarkResponse.class);
            response.setMobileImageRemarkDtoList(remarkListNew);
        } catch (Exception e) {
            LogUtils.error("MP32--拍照查勘-获取备注信息--失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    CheckImageRemarkResponse.class);
        } finally {
            // 返回结果封装
            result = JsonUtils.toJson(response);
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) LogUtils.info("MP32--拍照查勘-获取备注信息,响应结果：" + result);
            else LogUtils.error("MP32--拍照查勘-获取备注信息,响应结果：" + result);
            // 加密返回
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP50
     * 拍照查勘-Android上传图片或视频
     * 
     * @Title：照片上传（单证，照片补传，人伤预审核）
     * @author zhangyw 2016年10月14日 上午10:59:37
     * @param data
     * @return String
     */
    @RequestMapping("/uploadImageSurveyServlet78.action")
    @ResponseBody
    public String UploadImageSurveyServlet78(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP50入参=" + loguuid;
        UploadImageSurveyServlet78Request request = JsonUtils.fromJsonWithBase64(logTitle, data, UploadImageSurveyServlet78Request.class);
        UploadImageSurveyServlet78Response response = null;
        String imgId = UUID.randomUUID().toString().replace("-", "");
        String result = "";
        List<PrpmUploadLossItemImg> uploadList = new ArrayList<PrpmUploadLossItemImg>();
        PrpmUploadLossItemImg prpmUploadLossItemimg = new PrpmUploadLossItemImg();
        try {
            prpmUploadLossItemimg.setUploadLossItemImgId(imgId);
            prpmUploadLossItemimg.setUseCode(request.getUserId());
            prpmUploadLossItemimg.setLossItemCypId(request.getLossUuid());
            prpmUploadLossItemimg.setImgName(request.getImgName());
            prpmUploadLossItemimg.setImgPath(request.getImgPath());
            prpmUploadLossItemimg.setImgType(request.getImgType());
            String imgSource = request.getImgSource();
            if (StringUtils.isNotBlank(imgSource)) {
                if (StringUtils.equals("1", imgSource)) {
                    imgSource = "8";
                } else if (StringUtils.equals("2", imgSource)) {
                    imgSource = "9";
                } else if (StringUtils.equals("3", imgSource)) {
                    imgSource = "10";
                }
            }
            prpmUploadLossItemimg.setImgSource(imgSource);
            String shotPlace = request.getShotPlace();
            if (StringUtils.isNotBlank(shotPlace) && StringUtils.contains(shotPlace, ",")) {
                String[] strs = StringUtils.split(shotPlace, ",");
                prpmUploadLossItemimg.setShotLatitude(strs[0]);
                if (strs.length > 1) {
                    prpmUploadLossItemimg.setShotLongitude(strs[1]);
                }
            }
            if("C".equals(request.getProjectCode())){
                prpmUploadLossItemimg.setMobile(request.getUserId());
            }
            prpmUploadLossItemimg.setShotTime(DateUtil.strToDate(request.getShotTime()));
            int fileFlag = Integer.valueOf(request.getFileFlag());
            prpmUploadLossItemimg.setIsVideoImg(fileFlag);
            // request.getCaseNo();
            // request.getCaseUuid();
            // request.getSurveyDate();
            // request.getDelegateRemark();
            // request.getOtherRemark();
            prpmUploadLossItemimg.setOssKey(request.getOSSKey());
            prpmUploadLossItemimg.setUploadTime(new Date());
            prpmUploadLossItemimg.setLockFlag(0);
            prpmUploadLossItemimg.setStayUpload("1");
            prpmUploadLossItemimg.setCreateTime(new Date());
            prpmUploadLossItemimg.setCreateBy(request.getUserId());
            prpmUploadLossItemimg.setModifyTime(new Date());
            prpmUploadLossItemimg.setModifyBy(request.getUserId());
            uploadList.add(prpmUploadLossItemimg);
            if (uploadList != null) {
                int r = prpmUploadLossItemimgService.saveUploadList(uploadList);
                response = MobileReturnCodeSupport.buildReponse(r > 0 ? MobileReturnCodeSupport.CODE_SUCCESS : MobileReturnCodeSupport.CODE_FAIL,
                        MobileReturnCodeSupport.SYS_CODE, null, UploadImageSurveyServlet78Response.class);
            }
            if (StringUtils.isNotBlank(request.getLossUuid()) && StringUtils.isNotBlank(request.getCaseUuid())) {
                PrpmItemImgRela prpmItemImgRela = new PrpmItemImgRela();
                prpmItemImgRela.setItemImgRelaId(UUIDUtils.getUUIDStr());
                prpmItemImgRela.setCaseInfoId(request.getCaseUuid());
                prpmItemImgRela.setLossItemId(request.getLossUuid());
                prpmItemImgRela.setUploadImageId(imgId);
                prpmItemImgRela.setCreateBy(request.getUserCode());
                prpmItemImgRela.setCreateTime(new Date());
                itemImgRelaService.save(prpmItemImgRela);
            }
        } catch (Exception e) {
            LogUtils.error("MP50--照片上传（单证，照片补传，人伤预审核）--失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    UploadImageSurveyServlet78Response.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP50--照片上传（单证，照片补传，人伤预审核）--返回结果【" + result + "】");
            } else {
                LogUtils.error("MP50--照片上传（单证，照片补传，人伤预审核）--返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP51
     * 身份证、银行卡、驾驶证、行驶证、临牌等照片上传
     * 
     * @Title：C端 深度注册OCR采集-图片上传
     * @author zhangyw 2016年10月14日 上午10:59:37
     * @param data
     * @return String
     */
    @RequestMapping("/uploadOcrImageServlet51.action")
    @ResponseBody
    public String uploadOcrImageServlet51(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP51入参=" + loguuid;
        UploadOcrImageServlet51Request request = JsonUtils.fromJsonWithBase64(logTitle, data, UploadOcrImageServlet51Request.class);
        UploadOcrImageServlet51Response response = null;
        String result = "";
        PrpmUploadOcrImg prpmUploadImg = new PrpmUploadOcrImg();
        try {
            prpmUploadImg.setUploadOcrImgId(loguuid);
            prpmUploadImg.setUseCode(request.getUserId());
            prpmUploadImg.setMobile(request.getMobile());
            prpmUploadImg.setImgName(request.getImgName());
            prpmUploadImg.setImgPath(request.getImgPath());
            prpmUploadImg.setImgType(request.getImgType());
            prpmUploadImg.setLicenseNo(request.getLicenseNo());
            String shotPlace = request.getShotPlace();
            if (StringUtils.isNoneBlank(shotPlace) && shotPlace.contains(",")) {
                String[] strs = StringUtils.split(shotPlace, ",");
                prpmUploadImg.setShotLatitude(strs[0]);
                if (strs.length > 1) {
                    prpmUploadImg.setShotLongitude(strs[1]);
                }
            }
            prpmUploadImg.setShotTime(DateUtil.strToDate24(request.getShotTime()));
            request.getFileFlag();
            prpmUploadImg.setOssKey(request.getOSSKey());
            prpmUploadImg.setUploadTime(new Date());
            prpmUploadImg.setCreateBy(request.getMobile());
            prpmUploadImg.setCreateTime(new Date());
            prpmUploadImg.setModifyBy(request.getMobile());
            prpmUploadImg.setModifyTime(new Date());
            int r = prpmUploadService.saveUploadOcrImg(prpmUploadImg);// 数据存储
            response = MobileReturnCodeSupport.buildReponse(r > 0 ? MobileReturnCodeSupport.CODE_SUCCESS : MobileReturnCodeSupport.CODE_FAIL,
                    MobileReturnCodeSupport.SYS_CODE, null, UploadOcrImageServlet51Response.class);
        } catch (Exception e) {
            LogUtils.error("MP51--C端 深度注册OCR采集-图片上传--失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    UploadOcrImageServlet51Response.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP51--C端 深度注册OCR采集-图片上传--返回结果【" + result + "】");
            } else {
                LogUtils.error("MP51--C端 深度注册OCR采集-图片上传--返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP62
     * 查勘员，自助拍照查勘-Android，IOS上传图片或视频
     * 
     * @Title：上传影像
     * @author zhangyw 2016年10月14日 上午10:24:10
     * @param data
     * @return String
     */
    @RequestMapping("/uploadvideo.action")
    @ResponseBody
    public String uploadVideo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP62入参=" + loguuid;
        UploadVideoRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, UploadVideoRequest.class);
        UploadVideoResponse response = null;
        String result = "";
        try {
            if (StringUtils.isBlank(request.getCaseUuid())) {
                throw new Exception("案件唯一标识为空！");
            }
            String fileFlag = request.getFileFlag();// 文件类型： 0、图片；1、视频
            PrpmUploadImage prpmUploadImg = null;
            PrpmUploadVideo prpmUploadVideo = null;
            String UUID = UUIDUtils.getUUIDStr();
            String lossUuids = request.getLossUuid();
            String lossUuid = "";
            String[] lossUuidStrs = new String[] {};
            if (lossUuids != null && lossUuids.contains(",")) {
                lossUuidStrs = StringUtils.split(lossUuids, ",");
            } else {
                lossUuid = lossUuids;
                if (StringUtils.isNotBlank(lossUuid)) {
                    lossUuidStrs = new String[] { lossUuid };
                }
            }
            if ("0".equals(fileFlag)) {// 图片
                prpmUploadImg = new PrpmUploadImage();
                prpmUploadImg.setUserCode(request.getUserId());
                prpmUploadImg.setCaseNo(request.getCaseNo());
                prpmUploadImg.setUploadImageId(UUID);
                prpmUploadImg.setCaseInfoId(request.getCaseUuid());
                request.getLossId();
                prpmUploadImg.setLossItemId(lossUuid);
                request.getCaseType();
                prpmUploadImg.setImgName(request.getImgName());
                prpmUploadImg.setImgPath(request.getImgPath());
                prpmUploadImg.setImgType(request.getImgType());
                String shotPlace = request.getShotPlace();
                if (StringUtils.isNotBlank(shotPlace) && shotPlace.contains(",")) {
                    String[] strs = StringUtils.split(shotPlace, ",");
                    prpmUploadImg.setShotLatitude(strs[0]);
                    if (strs.length > 1) {
                        prpmUploadImg.setShotLongitude(strs[1]);
                    }
                }
                if (StringUtils.isNotBlank(request.getTel())) {
                    prpmUploadImg.setMobile(request.getTel());
                }
                prpmUploadImg.setShotTime(DateUtil.strToDate24(request.getShotTime()));
                prpmUploadImg.setLockFlag(Integer.parseInt(Str.chgStrZero(request.getLockFlag())));
                prpmUploadImg.setOssKey(request.getOSSKey());
                prpmUploadImg.setUploadTime(new Date());
                prpmUploadImg.setBreakPointFlag("1");
                prpmUploadImg.setStayUpload("1");
                prpmUploadImg.setLicenseNo(request.getLicenseNo());
                prpmUploadImg.setCreateTime(new Date());
                prpmUploadImg.setCreateBy(request.getUserCode());
                prpmUploadImg.setModifyTime(new Date());
                prpmUploadImg.setModifyBy(request.getUserCode());
            } else if ("1".equals(fileFlag)) {// 视频
                prpmUploadVideo = new PrpmUploadVideo();
                prpmUploadVideo.setUserCode(request.getUserCode());
                prpmUploadVideo.setCaseNo(request.getCaseNo());
                prpmUploadVideo.setCaseInfoId(request.getCaseUuid());
                prpmUploadVideo.setLossItemId(lossUuid);
                prpmUploadVideo.setVideoName(request.getImgName());
                // request.getLossId();
                // request.getCaseType();
                // request.getImgPath();
                // request.getShotPlace();
                // request.getShotTime();
                // request.getLockFlag();
                prpmUploadVideo.setStartTime(DateUtil.strToDate24(request.getStartTime()));
                prpmUploadVideo.setEndTime(DateUtil.strToDate24(request.getEndTime()));
                prpmUploadVideo.setDataPath(request.getOSSKey());
                prpmUploadVideo.setUploadVideoId(UUID);
                prpmUploadVideo.setModifyTime(new Date());
                prpmUploadVideo.setVideoType("2");
                prpmUploadVideo.setStayUpload("1");
                prpmUploadVideo.setCreateTime(new Date());
                prpmUploadVideo.setCreateBy(request.getUserCode());
                prpmUploadVideo.setModifyTime(new Date());
                prpmUploadVideo.setModifyBy(request.getUserCode());
                if (StringUtils.isNotBlank(request.getTel())) {
                    prpmUploadVideo.setMobile(request.getTel());
                }
                prpmUploadVideo.setStayUpload("1");
            }
            int r = prpmUploadService.prpmUpload(prpmUploadImg, prpmUploadVideo);
            response = MobileReturnCodeSupport.buildReponse(r > 0 ? MobileReturnCodeSupport.CODE_SUCCESS : MobileReturnCodeSupport.CODE_FAIL,
                    MobileReturnCodeSupport.SYS_CODE, null, UploadVideoResponse.class);
            // 新增入库
            if (lossUuidStrs != null) {
                for (int i = 0; i < lossUuidStrs.length; i++) {
                    lossUuid = lossUuidStrs[i];
                    if ("0".equals(fileFlag)) {// 图片
                        PrpmItemImgRela prpmItemImgRela = new PrpmItemImgRela();
                        prpmItemImgRela.setItemImgRelaId(UUIDUtils.getUUIDStr());
                        prpmItemImgRela.setCaseInfoId(request.getCaseUuid());
                        prpmItemImgRela.setLossItemId(lossUuid);
                        prpmItemImgRela.setUploadImageId(UUID);
                        prpmItemImgRela.setCreateBy(request.getUserCode());
                        prpmItemImgRela.setCreateTime(new Date());
                        itemImgRelaService.save(prpmItemImgRela);
                    } else if ("1".equals(fileFlag)) {// 视频
                        PrpmItemVideoRela prpmItemVideoRela = new PrpmItemVideoRela();
                        prpmItemVideoRela.setItemVideoRelaId(UUIDUtils.getUUIDStr());
                        prpmItemVideoRela.setCaseInfoId(request.getCaseUuid());
                        prpmItemVideoRela.setLossItemId(lossUuid);
                        prpmItemVideoRela.setUploadVideoId(UUID);
                        prpmItemVideoRela.setCreateBy(request.getUserCode());
                        prpmItemVideoRela.setCreateTime(new Date());
                        itemVideoRelaService.save(prpmItemVideoRela);
                    }
                }
            }
        } catch (Exception e) {
            LogUtils.error("MP62--上传影像--失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    UploadVideoResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP62--上传影像--返回结果【" + result + "】");
            } else {
                LogUtils.error("MP62--上传影像--返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP68
     * C端上传影像
     * 
     * @Title：上传影像
     * @author zhangyw 2016年10月14日 上午10:24:10
     * @param data
     * @return String
     */
    @RequestMapping("/uploadvideo4C.action")
    @ResponseBody
    public String uploadvideo4C(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP68入参=" + loguuid;
        UploadVideoRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, UploadVideoRequest.class);
        UploadVideoResponse response = null;
        String result = "";
        try {
            if (StringUtils.isBlank(request.getCaseUuid())) {
                throw new Exception("案件唯一标识为空！");
            }
            String fileFlag = request.getFileFlag();// 文件类型： 0、图片；1、视频
            PrpmUploadImage prpmUploadImg = null;
            PrpmUploadVideo prpmUploadVideo = null;
            String UUID = UUIDUtils.getUUIDStr();
            // 只存一笔任务号，将任务id组装成数组存到loss_ids中
            String lossUuid = null;
            String lossIds = null;
            StringBuffer buf = new StringBuffer("");
            List<String> lossUuidStrs = new ArrayList<String>();
            List<LossItemDto> lossItemList = request.getLossItemList();
            if (lossItemList != null && lossItemList.size() > 0) {
                lossUuid = lossItemList.get(0).getLossUuid();
                buf.append(lossItemList.get(0).getLossId());
                lossUuidStrs.add(lossUuid);
                for (int i = 1; i < lossItemList.size(); i++) {
                    buf.append(',');
                    buf.append(lossItemList.get(i).getLossId());
                    lossUuidStrs.add(lossItemList.get(i).getLossUuid());
                }
                lossIds = buf.toString();
            }
            if ("0".equals(fileFlag)) {// 图片
                prpmUploadImg = new PrpmUploadImage();
                prpmUploadImg.setUserCode(request.getUserId());
                prpmUploadImg.setCaseNo(request.getCaseNo());
                prpmUploadImg.setCaseInfoId(request.getCaseUuid());
                prpmUploadImg.setLossItemId(lossUuid);
                prpmUploadImg.setImgName(request.getImgName());
                prpmUploadImg.setImgPath(request.getImgPath());
                prpmUploadImg.setImgType(request.getImgType());
                String shotPlace = request.getShotPlace();
                if (StringUtils.isNotBlank(shotPlace) && StringUtils.contains(shotPlace, ",")) {
                    String[] strs = StringUtils.split(shotPlace, ",");
                    prpmUploadImg.setShotLatitude(strs[0]);
                    if (strs.length > 1) {
                        prpmUploadImg.setShotLongitude(strs[1]);
                    }
                }
                prpmUploadImg.setShotTime(DateUtil.strToDate24(request.getShotTime()));
                prpmUploadImg.setLockFlag(Integer.parseInt(Str.chgStrZero(request.getLockFlag())));
                prpmUploadImg.setOssKey(request.getOSSKey());
                prpmUploadImg.setUploadImageId(UUID);
                prpmUploadImg.setUploadTime(new Date());
                prpmUploadImg.setBreakPointFlag("1");
                prpmUploadImg.setStayUpload("1");
                prpmUploadImg.setCreateBy(request.getUserId());
                prpmUploadImg.setCreateTime(new Date());
                prpmUploadImg.setModifyBy(request.getUserId());
                prpmUploadImg.setModifyTime(new Date());
                if (StringUtils.isNotBlank(request.getTel())) {
                    prpmUploadImg.setMobile(request.getTel());
                }
            } else if ("1".equals(fileFlag)) {// 视频
                prpmUploadVideo = new PrpmUploadVideo();
                prpmUploadVideo.setUserCode(request.getUserCode());
                prpmUploadVideo.setCaseNo(request.getCaseNo());
                prpmUploadVideo.setCaseInfoId(request.getCaseUuid());
                prpmUploadVideo.setLossItemId(lossUuid);
                prpmUploadVideo.setVideoName(request.getImgName());
                // request.getImgPath();
                // request.getShotPlace();
                // request.getShotTime();
                // request.getLockFlag();
                prpmUploadVideo.setStartTime(DateUtil.strToDate24(request.getStartTime()));
                prpmUploadVideo.setEndTime(DateUtil.strToDate24(request.getEndTime()));
                prpmUploadVideo.setDataPath(request.getOSSKey());
                prpmUploadVideo.setUploadVideoId(UUID);
                prpmUploadVideo.setModifyTime(new Date());
                prpmUploadVideo.setVideoType("2");
                prpmUploadVideo.setStayUpload("1");
                prpmUploadVideo.setCreateBy(request.getUserId());
                prpmUploadVideo.setCreateTime(new Date());
                prpmUploadVideo.setModifyBy(request.getUserId());
                prpmUploadVideo.setModifyTime(new Date());
                // /prpmUploadVideo.setLossIds(lossIds);
                if (StringUtils.isNotBlank(request.getTel())) {
                    prpmUploadVideo.setMobile(request.getTel());
                }
                prpmUploadVideo.setStayUpload("1");
            }
            int r = prpmUploadService.prpmUpload(prpmUploadImg, prpmUploadVideo);
            response = MobileReturnCodeSupport.buildReponse(r > 0 ? MobileReturnCodeSupport.CODE_SUCCESS : MobileReturnCodeSupport.CODE_FAIL,
                    MobileReturnCodeSupport.SYS_CODE, null, UploadVideoResponse.class);
            // 新增入库
            if (lossUuidStrs != null) {
                for (int i = 0; i < lossUuidStrs.size(); i++) {
                    lossUuid = lossUuidStrs.get(i);
                    if ("0".equals(fileFlag)) {// 图片
                        PrpmItemImgRela prpmItemImgRela = new PrpmItemImgRela();
                        prpmItemImgRela.setItemImgRelaId(UUIDUtils.getUUIDStr());
                        prpmItemImgRela.setCaseInfoId(request.getCaseUuid());
                        prpmItemImgRela.setLossItemId(lossUuid);
                        prpmItemImgRela.setUploadImageId(UUID);
                        prpmItemImgRela.setCreateBy(request.getUserId());
                        prpmItemImgRela.setCreateTime(new Date());
                        itemImgRelaService.save(prpmItemImgRela);
                    } else if ("1".equals(fileFlag)) {// 视频
                        PrpmItemVideoRela prpmItemVideoRela = new PrpmItemVideoRela();
                        prpmItemVideoRela.setItemVideoRelaId(UUIDUtils.getUUIDStr());
                        prpmItemVideoRela.setCaseInfoId(request.getCaseUuid());
                        prpmItemVideoRela.setLossItemId(lossUuid);
                        prpmItemVideoRela.setUploadVideoId(UUID);
                        prpmItemVideoRela.setCreateBy(request.getUserId());
                        prpmItemVideoRela.setCreateTime(new Date());
                        itemVideoRelaService.save(prpmItemVideoRela);
                    }
                }
            }
        } catch (Exception e) {
            LogUtils.error("MP68--上传影像--失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    UploadVideoResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP68--C端上传影像--返回结果【" + result + "】");
            } else {
                LogUtils.error("MP68--C端上传影像--返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * @Title: upload图片或视频上传
     * @Description:
     * @param @param request
     * @param @param data
     * @param @return 设定文件
     * @return String 返回类型
     * @throws
     */
    // @RequestMapping("/upload.action")
    // @ResponseBody
    // public String upload(HttpServletRequest request, String data) {
    // String result = null;
    // BaseResponse response = null;
    // boolean resultFlg = false;
    // try {
    // // 获取参数
    // UploadImageSurveyRequest ur = JsonUtils.fromJson(data, UploadImageSurveyRequest.class);
    // // 创建一个通用的多部分解析器
    // CommonsMultipartResolver multipartResolver = new
    // CommonsMultipartResolver(request.getSession().getServletContext());
    // // 判断 request 是否有文件上传,即多部分请求
    // List<PrpmUploadImage> imageList = new ArrayList<PrpmUploadImage>();
    // if (multipartResolver.isMultipart(request)) {
    // // 转换成多部分request
    // MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
    // // 取得request中的所有文件名
    // Iterator<String> iter = multiRequest.getFileNames();
    // // 查询案件信息
    // PrpmCaseInfo caseInfo = mobileCaseService.getCaseInfoByUuid(ur.getCaseUuid());
    // PrpmUploadImage uploadImage = null;
    // if (caseInfo != null) {
    // while (iter.hasNext()) {
    // // 取得上传文件
    // MultipartFile file = multiRequest.getFile(iter.next());
    // System.out.println("文件名称......" + file.getOriginalFilename());
    // // 判断文件是否存在
    // if (file != null && StringUtils.isNotEmpty(file.getOriginalFilename())) {
    // uploadImage = saveUploadImage(ur, caseInfo.getOrgCode(), caseInfo.getOrgCode());
    // // 添加图片记录
    // imageList.add(uploadImage);
    // // 上传oss服务器
    // String res = ossManagerTool.uploadFile(file.getInputStream(), uploadImage.getOssKey(), uploadImage.getImgType());
    // LogUtils.info(ur.getImgName() + "，图片上传状态：" + res);
    // }
    // }
    // // 批量保存 prpmUploadImage
    // mobileImageService.saveBatchUploadImage(imageList);
    // // 更新案件上传标示
    // mobileCaseService.updateCaseInfoReFlag(caseInfo);
    // resultFlg = true;
    // }
    // }
    // if (!resultFlg) {
    // response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_CASENO_NO_ERROR,
    // MobileReturnCodeSupport.SYS_CODE, null);
    // } else {
    // response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS,
    // MobileReturnCodeSupport.SYS_CODE, null);
    // }
    // } catch (Exception e) {
    // LogUtils.error("上传图片异常信息：", e);
    // response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR,
    // MobileReturnCodeSupport.SYS_CODE, null);
    // }
    // // 返回结果封装
    // result = JsonUtils.toJson(response);
    // LogUtils.info("上传视频或图片,响应结果：" + result);
    // // 加密返回
    // result = EncryptUtil.encodeBase64(result);
    // return result;
    // }
    /**
     * @Title: 保存图片信息
     * @Description: 
     * @param @param request
     * @param @param companyCode
     * @param @param userCode
     * @param @return 设定文件
     * @return PrpmUploadImage 返回类型
     * @throws
     */
    private PrpmUploadImage saveUploadImage(UploadImageSurveyRequest request, String companyCode, String userCode) {
        PrpmUploadImage uploadImage = new PrpmUploadImage();
        uploadImage.setCaseNo(request.getCaseNo());
        uploadImage.setImgName(request.getImgName());
        uploadImage.setImgPath(request.getImgPath());
        uploadImage.setLicenseNo(request.getLicenseNo());
        uploadImage.setMobile(request.getMobile());
        // uploadImage.setShotPlace(request.getShotPlace());
        uploadImage.setShotTime(DateUtils.parseDate(request.getShotTime()));
        uploadImage.setUploadTime(new Date());
        uploadImage.setUserCode(userCode);
        uploadImage.setCaseNo(request.getCaseUuid());
        uploadImage.setOssKey("msplatform/image/" + companyCode + "/" + DateUtils.formatDate(new Date()) + "/" + request.getCaseNo() + "/"
                + request.getImgName());
        uploadImage.setUploadImageId(UUIDUtils.getUUIDStr());
        uploadImage.setLockFlag(Integer.valueOf(request.getLockFlag()));
        // uploadImage.setLossUuid(request.getLossUuid());
        uploadImage.setBreakPointFlag(request.getSupplyFlag());
        uploadImage.setRemark(request.getFileFlag());
        return uploadImage;
    }
}
